package com.zhht.htzx.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.nsgf.library.data.bean.BusinessSettleTypeBean;
import com.zhht.htzx.R;

import java.util.ArrayList;
import java.util.List;

public class RadioTagGroup extends LinearLayout {
    private List<Button> buttons = new ArrayList<>();
    private OnTagSelectedListener listener;
    private int selectedPosition = -1;

    public interface OnTagSelectedListener {
        void onTagSelected(int position, String tag);
    }

    public RadioTagGroup(Context context) {
        super(context);
        init();
    }

    public RadioTagGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadioTagGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
    }

    public void setTags(List<BusinessSettleTypeBean> tags) {
        removeAllViews();
        buttons.clear();
        selectedPosition = -1;

        for (int i = 0; i < tags.size(); i++) {
            Button button = createButton(tags.get(i).getName(), i);
            buttons.add(button);
            addView(button);
        }
    }

    private Button createButton(String text, final int position) {
        Button button = (Button) LayoutInflater.from(getContext())
                .inflate(R.layout.item_radio_tag, this, false);
        button.setText(text);
        button.setSelected(false);

        button.setOnClickListener(v -> {
            if (selectedPosition != position) {
                // 取消其他按钮的选中状态
                for (int i = 0; i < buttons.size(); i++) {
                    buttons.get(i).setSelected(i == position);
                }
                selectedPosition = position;
                if (listener != null) {
                    listener.onTagSelected(position, text);
                }
            }
        });

        return button;
    }

    public void setOnTagSelectedListener(OnTagSelectedListener listener) {
        this.listener = listener;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }

    public String getSelectedTag() {
        if (selectedPosition >= 0 && selectedPosition < buttons.size()) {
            return buttons.get(selectedPosition).getText().toString();
        }
        return null;
    }

    public void setSelectedPosition(int position) {
        if (position >= 0 && position < buttons.size()) {
            for (int i = 0; i < buttons.size(); i++) {
                buttons.get(i).setSelected(i == position);
            }
            selectedPosition = position;
        }
    }

    public void clearSelection() {
        for (Button button : buttons) {
            button.setSelected(false);
        }
        selectedPosition = -1;
    }
}